﻿using System.IO;
using System.Text.Json;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace SmartShell
{
    record R
    {
        public string work_dir { get; set; }
    }
    public partial class SmartShell : Form
    {
        private Process cmd;
        public SmartShell()
        {
            InitializeComponent();
        }

        private void SmartShell_Load(object sender, EventArgs e)
        {
            cmd = new Process();
            cmd.StartInfo.FileName = "cmd.exe";
            cmd.StartInfo.UseShellExecute = false;
            cmd.StartInfo.CreateNoWindow = true;

            cmd.StartInfo.RedirectStandardOutput = true;
            cmd.StartInfo.RedirectStandardError = true;
            cmd.StartInfo.RedirectStandardInput = true;

            try
            {
                string jStr = File.ReadAllText(Environment.CurrentDirectory + @"\config.json");
            }
            catch (Exception)
            {
                File.WriteAllText(Environment.CurrentDirectory + @"\config.json", @"{""work_dir"": """"}");
            }
            try
            {
                string jStr = File.ReadAllText(Environment.CurrentDirectory + @"\config.json");
                R r = JsonSerializer.Deserialize<R>(jStr);
                if (r.work_dir != null)
                {
                    cmd.StartInfo.WorkingDirectory = r.work_dir;
                    this.Text = r.work_dir;
                }
            }
            catch
            {
                File.WriteAllText(Environment.CurrentDirectory + @"\config.json", @"{""work_dir"": """"}");
            }
        }

        private void SmartShell_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                string jStr = File.ReadAllText(Environment.CurrentDirectory + @"\config.json");
                R r = JsonSerializer.Deserialize<R>(jStr);
                if (r.work_dir != this.Text)
                {
                    r.work_dir = this.Text;
                    jStr = JsonSerializer.Serialize<R>(r);
                    File.WriteAllText(Environment.CurrentDirectory + @"\config.json", jStr);
                }
            }
            catch
            {
                MessageBox.Show("Fuck");
            }
        }

        private void stdInput1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && stdInput1.Text != "")
            {
                cmd.Start();
                cmd.StandardInput.WriteLine(stdInput1.Text+"&exit");
                stdInput1.Items.Insert(0, stdInput1.Text);

                stdOut1.Text = cmd.StandardOutput.ReadToEnd();
                stdErr1.Text = cmd.StandardError.ReadToEnd();
                stdInput1.Text = "";
            }
        }

        private void 设定工作目录ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            if (folderBrowserDialog1.SelectedPath != "" && folderBrowserDialog1.SelectedPath != this.Text)
            {
                cmd.StartInfo.WorkingDirectory = folderBrowserDialog1.SelectedPath;
                this.Text = folderBrowserDialog1.SelectedPath;

                folderBrowserDialog1.SelectedPath = "";
            }
        }

        private void 在标准输入添加路径ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            if (folderBrowserDialog1.SelectedPath != "")
            {
                stdInput1.Text += folderBrowserDialog1.SelectedPath;
                folderBrowserDialog1.SelectedPath = "";
            }
        }

        private void 在标准输入添加文件路径ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            if (openFileDialog1.FileName != "")
            {
                stdInput1.Text += openFileDialog1.FileName;
                openFileDialog1.FileName = "";
            }
        }
    }
}
